<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-plus" size="small" @click="handleAdd"
          v-hasPermi="['system:annual:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="annualList" @selection-change="handleSelectionChange">
      <el-table-column label="缴费年度" align="center" prop="pay_year" />
      <el-table-column label="新入会单位时间" align="center" prop="new_club_time" />
      <el-table-column label="下发时间" align="center" prop="delivery_time" width="180">

      </el-table-column>
      <el-table-column label="每家缴费金额" align="center" prop="single_amount" />
      <el-table-column label="应缴费单位数" align="center" prop="should_unit_number" />
      <el-table-column label="实缴单位数" align="center" prop="actual_unit_number" />
      <el-table-column label="未缴费单位数" align="center" prop="none_unit_number" />
      <el-table-column label="应收总额" align="center" prop="receivable_amount" />
      <el-table-column label="实收总额" align="center" prop="receipts_amount" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:annual:edit']">终止缴费</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改年度会费对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="缴费年度" prop="payYear">
          <el-input v-model="form.payYear" placeholder="请输入缴费年度" />
        </el-form-item>
        <el-form-item label="缴费金额" prop="singleAmount">
          <el-input v-model="form.singleAmount" placeholder="请输入">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input type="textarea" :rows="2" placeholder="" v-model="textarea">
            当前共有{{ form.shouldUnitNumber }}家会员单位需要缴费，共计{{ form.receivableAmount }}元
          </el-input>
        </el-form-item>
        <el-form-item label="" prop="newClubTime">
          <!-- <el-input v-model="form.newClubTime" placeholder="请输入新入会单位时间" /> -->
          <el-date-picker clearable v-model="form.newClubTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择时间段">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAnnual, getAnnual, delAnnual, addAnnual, updateAnnual } from "@/api/annual/annual";

export default {
  name: "Annual",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 年度会费表格数据
      annualList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      textarea: "当前共有{{ shouldUnitNumber }}家会员单位需要缴费，共计{{ receivableAmount }}元,",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        payYear: null,
        newClubTime: null,
        deliveryTime: null,
        singleAmount: null,
        shouldUnitNumber: null,
        actualUnitNumber: null,
        noneUnitNumber: null,
        receivableAmount: null,
        receiptsAmount: null,
        status: null,
        deleteTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询年度会费列表 */
    getList() {
      this.loading = true;
      listAnnual(this.queryParams).then(response => {
        this.annualList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    //获取单位总数，获取单位需缴纳总额
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        payYear: null,
        newClubTime: null,
        deliveryTime: null,
        singleAmount: null,
        shouldUnitNumber: null,
        actualUnitNumber: null,
        noneUnitNumber: null,
        receivableAmount: null,
        receiptsAmount: null,
        status: null,
        createTime: null,
        updateTime: null,
        deleteTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新建年度会费";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAnnual(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改年度会费";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAnnual(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAnnual(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除年度会费编号为"' + ids + '"的数据项？').then(function () {
        return delAnnual(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/annual/export', {
        ...this.queryParams
      }, `annual_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
